﻿/* 
 ********************************************************************** 
 ** md5.h -- Header file for implementation of MD5                   ** 
 ** RSA Data Security, Inc. MD5 Message Digest Algorithm             ** 
 ** Created: 2/17/90 RLR                                             ** 
 ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version              ** 
 ** Revised (for MD5): RLR 4/27/91                                   ** 
 **   -- G modified to have y&~z instead of y&z                      ** 
 **   -- FF, GG, HH modified to add in last register done            ** 
 **   -- Access pattern: round 2 works mod 5, round 3 works mod 3    ** 
 **   -- distinct additive constant for each step                    ** 
 **   -- round 4 added, working mod 7                                ** 
 ********************************************************************** 
 */  
  
/* 
 ********************************************************************** 
 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** 
 **                                                                  ** 
 ** License to copy and use this software is granted provided that   ** 
 ** it is identified as the "RSA Data Security, Inc. MD5 Message     ** 
 ** Digest Algorithm" in all material mentioning or referencing this ** 
 ** software or this function.                                       ** 
 **                                                                  ** 
 ** License is also granted to make and use derivative works         ** 
 ** provided that such works are identified as "derived from the RSA ** 
 ** Data Security, Inc. MD5 Message Digest Algorithm" in all         ** 
 ** material mentioning or referencing the derived work.             ** 
 **                                                                  ** 
 ** RSA Data Security, Inc. makes no representations concerning      ** 
 ** either the merchantability of this software or the suitability   ** 
 ** of this software for any particular purpose.  It is provided "as ** 
 ** is" without express or implied warranty of any kind.             ** 
 **                                                                  ** 
 ** These notices must be retained in any copies of any part of this ** 
 ** documentation and/or software.                                   ** 
 ********************************************************************** 
 */

#ifndef _OSS_MD5_H_
#define _OSS_MD5_H_
#include "ossType.h"

#define OSS_MD5_MAX		    32
#define OSS_MD5_BIN_MAX		16

/* Data structure for MD5 (Message Digest) computation */  
typedef struct {  
    DWord i[2];                   /* number of _bits_ handled mod 2^64 */  
    DWord buf[4];                                    /* scratch buffer */  
    Byte in[64];                              /* input buffer */  
    Byte digest[16];     /* actual digest after MD5Final call */  
} OSS_MD5_CTX;

/**
 *		计算指定字符串的md5值
 *@data			待MD5的数据
 *@datalen		待md5数据的长度
 *@md5Value		OUT MD计算结果
 *@md5ValueMax	md5Value缓冲区大小(
                                     如果小于16字节，则返回失败；
                                     如果小区32字节则返回16字节的编码；
                                     如果大于等于32字节，则返回32字节的bcd编码)
 *@return		成功返回md5Value的使用字节数
 *				失败返回 -1
 *@notes		一般情况下，MD5的结果为16或32字节，32字节为16字节的bcd编码。
 */
int OssMd5(Byte* data,int datalen,Byte* md5Value,int md5ValueMax);

/**
 *		计算指定文件的md5值
 *@pcFilePath		待计算MD5的文件路径
 *@md5Value		OUT MD计算结果
 *@md5ValueMax	md5Value缓冲区大小(
                                如果小于16字节，则返回失败；
                                如果小区32字节则返回16字节的编码；
                                如果大于等于32字节，则返回32字节的bcd编码)
 *@return		成功返回md5Value的使用字节数
 *				失败返回 -1
 *@notes		一般情况下，MD5的结果为16或32字节，32字节为16字节的bcd编码。
 */
int OssCalcFileMd5(char* pcFilePath,Byte* md5Value,int md5ValueMax);

/**
 *      Md5初始化
 *@mdContext        md5上下文
 *@return           无
 */
void OssMd5Init (OSS_MD5_CTX *mdContext);

/**
 *      执行MD计算
 *@mdContext        MD5上下文，OssMd5Init初始化过。
 *@inBuf            待MD5计算的内容
 *@inLen            内容长度
 */
void OssMd5Update (OSS_MD5_CTX *mdContext, Byte *inBuf, DWord inLen);

/**
 *      完成MD计算
 */
void OssMd5Final (OSS_MD5_CTX *mdContext);



/**
 *		随机数Md5加密
 *@plaintext		加密前的明文
 *@plaintextLen		明文长度
 *@randtext			随机内容
 *@randtextLen		随机内容长度
 *@encryption		存放密文的空间
 *@encryptionMax	密文空间最大容量
 *@return			成功返回加密后的内容长度
 *					失败返回 -1
 */
int OssRandMd5(char* plaintext,int plaintextLen,char* randtext,int randtextLen,char* encryption,int encryptionMax);

#endif